package com.mm.utils;

import com.mm.plugin.logging.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 统一数据返回
 */
@Data
public class ApiResult<T> {
    @ApiModelProperty(value = "结果码",example = "200")
    private int code;
    @ApiModelProperty(value = "描述",example = "用户名已存在!")
    private String message;
    @ApiModelProperty(value = "返回的数据",example = "{\"name\":\"测试数据\",\"pwd\":\"测试测试\"}")
    private T data;

    public ApiResult() {
    }

    public ApiResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public void setResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public void setResultCode(ResultCodeEnum code) {
        this.code = code.getCode();
        this.message = code.getMessage();
    }

    /**
     *  成功
     */
    public static ApiResult success() {
        return new ApiResult(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),null);
    }

    /**
     * 成功 返回数据
     * @param data
     * @return
     */
    public static ApiResult success(Object data) {
        return new ApiResult(ResultCodeEnum.SUCCESS.getCode(),null,data);
    }

    /**
     * 返回失败
     */
    public static ApiResult fail() {
        return new ApiResult(ResultCodeEnum.FAIL.getCode(),ResultCodeEnum.FAIL.getMessage(),null);
    }

    /**
     * 返回失败
     */
    public static ApiResult fail(ResultCodeEnum code) {
        return new ApiResult(code.getCode(),code.getMessage(),null);
    }

    /**
     * 返回失败 自定义
     */
    public static ApiResult fail(int code, String message) {
        return new ApiResult(code,message,null);
    }


}
